<template>
    <section class="main">
        <div class="container">
            <div class="drag-box">
                <div class="drag-box-item">
                    <div class="item-title">商品分类</div>
                    <draggable v-model="todo">
                        <transition-group
                            tag="div"
                            id="todo"
                            class="item-ul"
                        >
                            <div
                                v-for="(item ,index) in todo"
                                class="drag-list"
                                
                                :key="item.id"
                            >
                                <a @click="setName(item)">
                                    {{item.name}}
                                </a>
                                <i
                                    style="float: right;"
                                    @click="removeShelf(index)"
                                    class="el-icon-delete"
                                ></i>
                </div>
                </transition-group>
                </draggable>
            </div>
            <div>
                <el-button
                    @click="add"
                    type="primary"
                >新增</el-button>
                    <el-button
                        @click="updateshelf"
                        type="primary"
                    >提交</el-button>
            </div>
        </div>
        </div>
    </section>
</template>

<script>
import { getShopShelf, updateshelfbya } from '../../api/index';
import draggable from 'vuedraggable';
export default {
    name: 'draglist',
    data() {
        return {
            dragOptions: {
                animation: 120,
                scroll: true,
                group: 'sortlist',
                ghostClass: 'ghost-style'
            },
            shopId: 0,
            todo: []
        };
    },
    components: {
        draggable
    },
    created() {
        this.shopId = localStorage.getItem('ms_shopId');
        this.dogetShopShelf();
    },
    methods: {
        //移除分类
        removeShelf(index) {
            var _this = this;
            _this.todo.splice(index, 1);
        },
        dogetShopShelf() {
            getShopShelf().then(e => {
                this.todo = e.data;
            });
        },
        setName(e) {
            this.$prompt('请输入货架名称', '提示', {
                inputValue: e.shelfName,
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            })
                .then(({ value }) => {
                    e.name = value;
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消修改'
                    });
                });
        },
        updateshelf() {
            for (let i = 0; i < this.todo.length; i++) {
                this.todo[i].rank = i;
            }
            updateshelfbya(this.todo).then(e => {
                if (e.statusCode == 200) {
                    this.$message.success('更新成功');
                }
            });
        },
        add() {
            this.$prompt('请输入货架名称', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            })
                .then(({ value }) => {
                    this.todo.push({
                        id: '',
                        name: value,
                        rank: 0
                    });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消新增'
                    });
                });
        },
        removeHandle(event) {
            console.log(event);
            this.$message.success(`从 ${event.from.id} 移动到 ${event.to.id} `);
        }
    }
};
</script>

<style scoped>
.drag-box {
    display: flex;
    user-select: none;
}
.drag-box-item {
    flex: 1;
    max-width: 330px;
    min-width: 300px;
    background-color: #eff1f5;
    margin-right: 16px;
    border-radius: 6px;
    border: 1px #e1e4e8 solid;
}
.item-title {
    padding: 8px 8px 8px 12px;
    font-size: 14px;
    line-height: 1.5;
    color: #24292e;
    font-weight: 600;
}
.item-ul {
    padding: 0 8px 8px;
    height: 500px;
    overflow-y: scroll;
}
.item-ul::-webkit-scrollbar {
    width: 0;
}
.drag-list {
    border: 1px #e1e4e8 solid;
    padding: 10px;
    margin: 5px 0 10px;
    list-style: none;
    background-color: #fff;
    border-radius: 6px;
    cursor: pointer;
    -webkit-transition: border 0.3s ease-in;
    transition: border 0.3s ease-in;
}
.drag-list:hover {
    border: 1px solid #20a0ff;
}
.drag-title {
    font-weight: 400;
    line-height: 25px;
    margin: 10px 0;
    font-size: 22px;
    color: #1f2f3d;
}
.ghost-style {
    display: block;
    color: transparent;
    border-style: dashed;
}
</style>
